package com.xherorlclass;

import com.xherorlclass.util.zookeeper.ZookeeperNode;
import com.xherorlclass.util.zookeeper.ZookeeperUtils;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooKeeper;

import java.util.Arrays;
import java.util.List;

/**
 * @author ：Xherorl
 * @date ：Created in 2023/8/24 15:38
 * @description：
 */

/*
* 注册中心的管理页面
* */
public class Application {

    public static void main(String[] args) throws InterruptedException{
        // 帮我们创建基础目录
        // yrpc-metadata   (持久节点)
        //  └─ providers （持久节点）
        //  		└─ service1  （持久节点，接口的全限定名）
        //  		    ├─ node1 [data]     /ip:port
        //  		    ├─ node2 [data]
        //            └─ node3 [data]
        //  └─ consumers
        //        └─ service1
        //             ├─ node1 [data]
        //             ├─ node2 [data]
        //             └─ node3 [data]
        //  └─ config

        // 创建一个zookeeper实例
        ZooKeeper zookeeper = ZookeeperUtils.createZookeeper();

        // 定义节点和数据
        String basePath = "/xherorlrpc-metadata";
        String providerPath = basePath + "/providers";
        String consumersPath = basePath + "/consumers";

        ZookeeperNode baseNode = new ZookeeperNode(basePath, null);
        ZookeeperNode providersNode = new ZookeeperNode(providerPath, null);
        ZookeeperNode consumersNode = new ZookeeperNode(consumersPath, null);

        // 创建节点
        List<ZookeeperNode> nodes = Arrays.asList(baseNode, providersNode, consumersNode);
        for (ZookeeperNode node : nodes) {
            ZookeeperUtils.createNode(zookeeper, node, null, CreateMode.PERSISTENT);
        }

        // 关闭连接
        ZookeeperUtils.close(zookeeper);

    }
}
